﻿@inherits NITASA.Views.AutofacEnabledViewPage<List<Tuple<Role, int>>>
@using NITASA.Data
@using GridMvc.Html
@using NITASA.Services.Security
@{
    ViewBag.Title = "Roles";
}

        <div class="main-content">
            <div class="container-fluid padded">
                @if (TempData["SuccessMessage"] != null)
                {
                    <div class="alert alert-success">
                        <button class="close" data-dismiss="alert" type="button">
                            ×
                        </button>
                        @TempData["SuccessMessage"]
                    </div>
                }
                @if (TempData["ErrorMessage"] != null)
                {
                    <div class="alert alert-error">
                        <button class="close" data-dismiss="alert" type="button">
                            ×
                        </button>
                        @TempData["ErrorMessage"]
                    </div>
                }
                <div class="row-fluid">
                    <div class="span12">
                        <div class="box">
                            <div class="box-header">
                                <span class="title">Browse Roles</span>
                                @*@if (aclService.HasRight(Rights.CreateNewRoles))*@
                                @if (aclService.HasRight(Rights.CreateNewRoles))
                                {
                                    <ul class="box-toolbar">
                                        <li><a href="@Url.Action("Add")" title="Add New Role"><i class="icon-plus-sign" style="margin-top: 10px"></i></a></li>
                                    </ul>
                                }
                            </div>
                            <div class="box-content">
                                @if (Model.Count() > 0)
                                {
                                    @Html.Grid(Model).Columns(columns =>
                                    {
                                        columns.Add(x => x.Item1.Name).Titled("Role Name").Sortable(true).Filterable(true);
                                        columns.Add(x => x.Item2).Titled("No. of Users").Sortable(true).Filterable(true);
                                        columns.Add(x => x.Item1.AddedOn).Titled("Added On").Sortable(true).Filterable(true);
                                        columns.Add().SetWidth(110).RenderValueAs(x => RenderButtons(x.Item1, aclService)).Sanitized(false).Encoded(false).Titled("");
                                    }).WithPaging(10, 5).WithMultipleFilters()
                                }
                                else
                                {
                                    <div class="padded">
                                        No Roles Found.
                                    </div>
                                }
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
@helper RenderButtons(Role item,IAclService aclService)
{
    if (item.Name != "Administrator")
    {
        if (NITASA.Areas.Admin.Helper.Functions.CurrentUserID() == item.AddedBy)
        {
            //if (aclService.HasRight(Rights.EditOwnRoles))
            if (aclService.HasRight(Rights.EditOwnRoles))
            {
                @Html.ActionLink("Edit", "Edit", new { GUID = item.GUID }, new { @class = "btn btn-small btn-blue" })
            }
        }
        else
        {
            //if (aclService.HasRight(Rights.EditOtherUsersRoles))
            if (aclService.HasRight(Rights.EditOtherUsersRoles))
            {
                @Html.ActionLink("Edit", "Edit", new { GUID = item.GUID }, new { @class = "btn btn-small btn-blue" })
            }
        }
        //if (aclService.HasRight(Rights.DeleteRoles))
        if (aclService.HasRight(Rights.DeleteRoles))
        {
            @Html.ActionLink("Delete", "Delete", new { GUID = item.GUID }, new { @class = "btn btn-small btn-red", onclick = "return confirm('Are you sure to delete this Role ?');" })
        }
    }
}
